package com.atguigu.eduservice.controller;


import com.atguigu.comMonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.impl.EduChapterServiceImpl;
import com.atguigu.exceptionHandler.GuliException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * <p>
 * 课程章节 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-04-07
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Slf4j
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;


    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable("courseId") String courseId) {
        log.info(courseId);
        ArrayList chapterVideo = eduChapterService.getChapterVideo(courseId);
        return R.ok().data("allChapterVideo", chapterVideo);
    }

    /**
     * 根据课程id,保存章节
     * @param eduChapter
     * @return
     */
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        boolean save = eduChapterService.save(eduChapter);
        return R.ok();
    }


    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        boolean save = eduChapterService.updateById(eduChapter);
        return R.ok();
    }
    @DeleteMapping("deleteChapter/{courseId}")
    public R deleteChapter(@PathVariable String courseId) {
        boolean save = eduChapterService.isExitisChild(courseId);
        if (save) {
            return R.ok();
        }
        return R.error();
    }


    @GetMapping("findChapter/{courseId}")
    public R updateChapter(@PathVariable String courseId) {
        EduChapter byId = eduChapterService.getById(courseId);
        return R.ok().data("chapter",byId);
    }
}

